<template>
  <!-- 内容 -->
  <div class="main">
    <div class="container pl-5 pr-5 pt-3 pb-3 mt-3 mb-3">
      <h3 class="text-center text-info border-bottom pb-3">注&nbsp;&nbsp;册</h3>
      <form class="mt-5" @submit.prevent="register">
        <div class="form-group row">
          <label for="username" class="col-sm-2 col-form-label text-right">账号:</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="username" placeholder="请输入您的账号!" required v-model="username">
            <div class="invalid-feedback">
              {{ usernameMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <label for="password" class="col-sm-2 col-form-label text-right">密码:</label>
          <div class="col-sm-10">
            <input type="password" class="form-control" id="password" placeholder="请输入您的密码!" required v-model="password">
            <div class="invalid-feedback">
              {{ passwordMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <label for="confirm-password" class="col-sm-2 col-form-label text-right">确认密码:</label>
          <div class="col-sm-10">
            <input type="password" class="form-control" id="confirm-password" placeholder="请再次输入密码!" required
            v-model="confirmPassword" @input="changeConfirmMsg">
            <div class="invalid-feedback">
              {{ confirmPasswordMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <label for="email" class="col-sm-2 col-form-label text-right">邮箱:</label>
          <div class="col-sm-10">
            <input type="email" class="form-control" id="email" placeholder="请输入您的邮箱!" required v-model="email">
            <div class="invalid-feedback">
              {{ emailMsg }}
            </div>
          </div>
        </div>
        <div class="form-group row mt-4">
          <div class="col-sm-2"></div>
          <div class="col-sm-10 text-center">
            <button type="submit" class="btn btn-info text-white form-control">立即注册</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import User from '@/api/user'
import router from '@/router'

const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const email = ref('')

const usernameMsg = ref('')
const passwordMsg = ref('')
const confirmPasswordMsg = ref('')
const emailMsg = ref('')

const changeConfirmMsg = () => {
  if (confirmPassword.value != password.value) {
    confirmPasswordMsg.value = '两次输入的密码不一致!'
  }
}

const register = async () => {
  await User.register(username.value, password.value, email.value)
    .then((_res) => {
      // 跳转到激活邮箱的中转页面
      router.replace('activation')
    }).catch((res) => {
      // 注册失败
      usernameMsg.value = res.usernameMsg ?? ''
      passwordMsg.value = res.passwordMsg ?? ''
      emailMsg.value = res.emailMsg ?? ''
    })
}
</script>

<style scoped>
</style>
